package server_demo

import (
	"google.golang.org/grpc"
	"grpc_demo/service"
	"net"
)
// 抽象接口
type srv interface {
	Register(server *grpc.Server) error
}

type DemoServiceSvc struct {
	srv
}
// 用户自定义服务，都实现一下srv接口，然后注册自己服务到 grpc server
func (dss DemoServiceSvc) Register(server *grpc.Server) error {
	service.RegisterDemoServiceServer(server,&service.SayHelloServer{})
	return nil
}
// 简单抽象
type AgentServer struct {
	svcList []srv
	addr string
	lis net.Listener
	server *grpc.Server
}



func NewAgentServer(addr string) (*AgentServer,error){
	tlis,err:=net.Listen("tcp", addr)
	if err!=nil{
		return &AgentServer{} , err
	}
	return &AgentServer{
		addr: addr,
		lis: tlis,
		server: grpc.NewServer(),
	},nil
}

func (as *AgentServer) RegisterSVC(service srv) error {
	as.svcList = append(as.svcList,service)
	return nil
}

func (as *AgentServer) Start() {
	for _,s := range as.svcList{
		s.Register(as.server)
	}
	as.server.Serve(as.lis)
}